<template>
  <div class="container mt-5">
    <div class="row mb-4">
      <div class="col-12">
        <h2 class="mb-3"><i class="fas fa-donate mr-2"></i>捐赠管理</h2>
        <div class="d-flex justify-content-between align-items-center mb-3">
          <div class="search-container">
            <div class="search-box">
              <input type="text" v-model="searchQuery" class="search-input" placeholder="搜索捐赠人、用途...">
              <button class="search-btn" @click="searchDonation">
                <i class="fas fa-search"></i>
              </button>
            </div>
          </div>
          <router-link to="/donation/add" class="btn btn-primary">
            <i class="fas fa-plus mr-1"></i>添加捐赠记录
          </router-link>
        </div>
      </div>
    </div>

    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-body">
            <div class="table-responsive">
              <table class="table table-hover">
                <thead>
                  <tr>
                    <th>捐赠人</th>
                    <th>捐赠金额</th>
                    <th>捐赠时间</th>
                    <th>捐赠用途</th>
                    <th>状态</th>
                    <th>操作</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="item in donationList" :key="item.id">
                    <td>{{ item.donor }}</td>
                    <td>{{ formatAmount(item.amount) }}</td>
                    <td>{{ formatDate(item.donationDate) }}</td>
                    <td>{{ item.purpose }}</td>
                    <td>{{ item.status }}</td>
                    <td>
                      <router-link :to="`/donation/edit/${item.id}`" class="btn btn-sm btn-warning mr-2">
                        <i class="fas fa-edit"></i>
                      </router-link>
                      <button @click="deleteDonation(item.id)" class="btn btn-sm btn-danger">
                        <i class="fas fa-trash"></i>
                      </button>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DonationList',
  data() {
    return {
      donationList: [],
      searchQuery: ''
    }
  },
  methods: {
    formatAmount(amount) {
      return `¥${amount.toFixed(2)}`
    },
    formatDate(date) {
      return new Date(date).toLocaleDateString()
    },
    async fetchDonationList() {
      try {
        const response = await fetch('/api/donation/list')
        const data = await response.json()
        this.donationList = data
      } catch (error) {
        console.error('获取捐赠列表失败:', error)
      }
    },
    searchDonation() {
      // TODO: 实现搜索捐赠记录的API调用
      console.log('搜索捐赠:', this.searchQuery)
      // 模拟搜索结果
      this.donationList = this.donationList.filter(item =>
        item.donor.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
        item.purpose.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    },
    async deleteDonation(id) {
      if (!confirm('确定要删除该捐赠记录吗？')) return

      try {
        await fetch(`/api/donation/delete/${id}`, {
          method: 'DELETE'
        })
        this.fetchDonationList()
      } catch (error) {
        console.error('删除捐赠记录失败:', error)
      }
    }
  },
  created() {
    this.fetchDonationList()
  }
}
</script>

<style scoped>
/* 卡片样式 */
.card {
  border: none;
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  background: linear-gradient(145deg, #ffffff 0%, #f8f9fa 100%);
}

/* 卡片头部样式 */
.card-header {
  background-color: #2c3e50;
  color: white;
  border-bottom: none;
  padding: 1.5rem 1.5rem 1rem;
  border-radius: 15px 15px 0 0;
}

.card-header h2 {
  margin: 0;
  font-size: 1.8rem;
  font-weight: 600;
  display: flex;
  align-items: center;
}

.card-header i {
  color: #3498db;
  margin-right: 0.5rem;
  font-size: 1.2rem;
}

/* 添加按钮样式 */
.btn-primary {
  background-color: #3498db;
  border: none;
  padding: 0.75rem 1.5rem;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-primary:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.2);
}

/* 表格样式 */
.table {
  margin-bottom: 0;
  background: white;
}

.table th {
  background-color: #f8f9fa;
  border: none;
  font-weight: 600;
  color: #333;
  padding: 1rem;
}

.table td {
  border: none;
  vertical-align: middle;
  padding: 1rem;
  font-size: 0.95rem;
}

.table tr:hover {
  background-color: rgba(52, 152, 219, 0.05);
}

/* 操作按钮样式 */
.btn-sm {
  padding: 0.4rem 0.8rem;
  font-size: 0.875rem;
  border-radius: 6px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.btn-sm i {
  font-size: 0.875rem;
}

.btn-warning {
  background-color: #f1c40f;
  border: none;
}

.btn-danger {
  background-color: #e74c3c;
  border: none;
}

.btn-warning:hover,
.btn-danger:hover {
  opacity: 0.9;
  transform: translateY(-1px);
}

/* 搜索框样式 */
.search-container {
  max-width: 400px;
  margin-bottom: 1rem;
}

.search-box {
  position: relative;
  display: flex;
  align-items: center;
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.search-box:hover {
  border-color: #3498db;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.search-input {
  flex: 1;
  border: none;
  outline: none;
  padding: 0.75rem 1rem;
  font-size: 0.95rem;
  background: transparent;
  color: #333;
  transition: all 0.3s ease;
}

.search-input:focus {
  box-shadow: none;
  border: none;
  outline: none;
  background: white;
}

.search-btn {
  background-color: #3498db;
  border: none;
  color: white;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 500;
}

.search-btn:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.2);
}

.search-btn i {
  font-size: 1rem;
}

.search-box::before {
  content: '';
  position: absolute;
  left: 1rem;
  width: 20px;
  height: 20px;
  background: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20'%3E%3Cpath fill='%236c757d' d='M8 16a8 8 0 1 0 0-16 8 8 0 0 0 0 16zm.93-9.41a3 3 0 0 0-4.24-4.24l-2.15 2.15a4 4 0 0 1 5.66 5.66l.75-.75A3 3 0 0 0 8 9.41z'/%3E%3C/svg%3E") no-repeat center center;
  background-size: contain;
  opacity: 0.5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-container {
    width: 100%;
    margin-bottom: 1rem;
  }
  .search-input,
  .search-btn {
    font-size: 0.9rem;
    padding: 0.65rem 0.85rem;
  }
  .search-btn i {
    font-size: 0.95rem;
  }
  .card-header h2 {
    font-size: 1.5rem;
  }
  .btn-primary {
    padding: 0.6rem 1.2rem;
    font-size: 0.9rem;
  }
  .table td {
    padding: 0.8rem;
  }
}

/* 其他间距类 */
.mr-2 {
  margin-right: 0.75rem;
}

.mr-1 {
  margin-right: 0.5rem;
}

  /* 其他间距类 */
  .mr-2 {
    margin-right: 0.75rem;
  }

  .mr-1 {
    margin-right: 0.5rem;
  }

  /* 响应式设计 */
  @media (max-width: 768px) {
    .card-header h2 {
      font-size: 1.5rem;
    }
    
    .btn-primary {
      padding: 0.6rem 1.2rem;
      font-size: 0.9rem;
    }
    
    .table td {
      padding: 0.8rem;
    }
  }
</style>
